package org.shijiagen.category.controller;

import lombok.RequiredArgsConstructor;
import org.shijiagen.category.service.CategoryService;
import org.shijiagen.param.ProductHotParam;
import org.shijiagen.pojo.Category;
import org.shijiagen.utils.R;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @GetMapping("promo/{categoryName}")
    public R byName(@PathVariable String categoryName){
        if(StringUtils.hasText(categoryName)){
            Category category = categoryService.byName(categoryName);
            return category != null ? R.ok("类别查询成功！",category):R.fail("找不到该类别！");
        }
        return R.fail("类别名称不能为空！");
    }

    /**
     * 热门类别id查询
     * @param productHotParam
     * @param
     * @return
     */
    @PostMapping("hots")
    public R hotsCategory(@RequestBody @Validated ProductHotParam productHotParam){
        return R.ok("类别集合查询成功！",categoryService.hotsCategory(productHotParam));
    }

    @GetMapping("list")
    public R list(){
        List<Category> list = categoryService.list();
        return R.ok("类别全部数据查询成功！",list);
    }
}
